/*
 *
 * Version 1.00
 * 
 * Created on 2007-6-4
 * Encoding UTF-8
 */
package net.comm.user.web.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import net.comm.common.GlbFinal;
import net.comm.common.ReturnMessage;

import net.comm.user.service.impl.UserSetServiceImpl;
import net.comm.user.web.form.UserSetForm;
import net.comm.util.HtmlMessageBox;
import net.comm.web.base.AbstractBaseAction;


/**
 *会员设置Action
 * @author hcgong(hcgong@sina.com)
 * @version 1.0
 * @date 2007-6-4
 */
public class UserSetAction extends AbstractBaseAction {
	
	private UserSetServiceImpl service = null;
	
	/**
	 * struct-config中该ACTION中的每个forward的常量定义
	 */
	final String OPER_FORWARD = "oper";
	final String ABOUTME_FORWARD = "aboutme";
	final String LOGIN_FORWARD = "loginFwd";
	final String SENDMAIL_FORWARD = "sendmail";
	final String RESENDMAIL_FORWARD = "resendmail";
	
	
	/**
	 *构造方法
	 */
	public UserSetAction()
	{
		super(false);
		
		/**
		 * 初始化业务组件接口
		 */
		service = new UserSetServiceImpl();
	}
	
	/**
	 * 默认动作
	 * 1.action值为空的时候,进入查询页面
	 * 2.每个操作都执行的代码,但是方法必须返回null
	 * @param mapping	 跳转的mapping值	
	 * @param request	 HttpServletRequest对象
	 * @param response	 HttpServletResponse对象
	 */
	public ActionForward startAction(ActionMapping mapping, ActionForm form,
									 HttpServletRequest request , 
									 HttpServletResponse response)
	{
		String action = getReqParam(request , ACTION_KEY);
		if(action == null || action.trim().length() == 0) {
			return mapping.findForward(OPER_FORWARD);
		} else {
			return null;
		}
	}	

	/**
	 *取得会员设置
	 */
	public ActionForward get(ActionMapping mapping, ActionForm form,
							 HttpServletRequest request , 
							 HttpServletResponse response)
		throws Exception
	{
		UserSetForm theForm = (UserSetForm) form;
		
		int userID = (Integer)request.getSession().getAttribute(GlbFinal.SESSION_USERID);
			
		if(userID <= 0){
			
			return mapping.findForward(LOGIN_FORWARD);
			
		}
		theForm.setUserID(Integer.valueOf(userID));

		ReturnMessage msg  = new ReturnMessage();
		
		service.get(theForm, msg);
		if(msg.getErrCode() != GlbFinal.ERRCODE_SUCCESS) 
		{
			HtmlMessageBox hmb = new HtmlMessageBox();
			hmb.setAlertType(HtmlMessageBox.ALERTTYPE_CALLALERTMETHOD);
			hmb.setPrintMessage(msg.getMsg());
			hmb.setForwardUrl("javascript:history.go(-1)");
			return showMsg(hmb , request , response , mapping);
		}
		request.setAttribute("theForm", theForm);

		return mapping.findForward(OPER_FORWARD);

	}	

	/**
	 * 跳转到显示页面
	 */
	public ActionForward modifyPage(ActionMapping mapping, ActionForm form,
			 				 	 HttpServletRequest request , 
			 				 	 HttpServletResponse response)  
		throws Exception
	{
	
		return get(mapping , form, request , response);
	}	

	/**
	 *修改会员设置
	 */
	public ActionForward update(ActionMapping mapping, ActionForm form,
							 HttpServletRequest request , 
							 HttpServletResponse response)
		throws Exception
	{
		UserSetForm theForm = (UserSetForm) form;
		
		ReturnMessage msg  = new ReturnMessage();
		
		service.update(theForm, msg);
		if(msg.getErrCode() != GlbFinal.ERRCODE_SUCCESS) 
		{
			HtmlMessageBox hmb = new HtmlMessageBox();
			hmb.setAlertType(HtmlMessageBox.ALERTTYPE_CALLALERTMETHOD);
			hmb.setPrintMessage(msg.getMsg());
			hmb.setForwardUrl("javascript:history.go(-1)");
			return showMsg(hmb , request , response , mapping);
		}
		
		request.setAttribute("theForm", theForm);
		
		return mapping.findForward(OPER_FORWARD);

	}

}
